
set more off

set seed 06031974

	forval b= 1(1)10 {

	di `b'
	
	use Data/Andrea/PV_forboot.dta, clear
	
	bsample , cluster(sid)
	
	
	quietly reg perkW_energy_generate i(6/18).hour##c.temp_interp i(6/18).hour##c.ghi i(6/18).hour#i(2/4).season##c.latitude i(6/18).hour#i(2/4).season##c.longitude
	eststo reg4f

use Data/Andrea/sample_forboot_PV.dta, clear


	bsample , cluster(account_number) idcluster(id)
	drop household_hour
	quietly predict PV_predictionkWh if hour >= 6 & hour<= 19 


** censor PV production at zero
	quietly gen PV_predict_cenkWh = PV_prediction 
	quietly replace PV_predict_cenkWh = 0 if PV_prediction <0

	quietly gen PV_productionkWh = PV_predict_cen* sizekW

	quietly gen unobserved_use_kWh = PV_production - exportkWh

	quietly gen consumption_kWh =  unobserved_use_kWh + importkWh
	quietly replace consumption = importkWh if unobserved_use_kWh ==.

	quietly replace PV_production = 0 if PV_production ==. &( hour<6 | hour>19)

	quietly gen virtualincome = price * PV_production

	quietly by id hour (read_date), sort:  gen lagged_2daysolar = PV_production[_n-2] if read_date[_n-2] == read_date[_n] -2

	
*** create hourly production

	
	forval x = 1(1)24 {
		
		quietly gen hour_meanirrad_PV`x' = hour_meanghi`x' * PV_production // hour by irradiance by solar production
		quietly gen hour_angle_PV`x' = hours`x' * PV_production * (streetangle ~=3) // hour by streetangle by solar production			
	
		}
		
forval x = 7(1)19 {
	
	quietly gen hour_sun_PFIT`x' = PV_productionkWh * (FIT_t=="P")* hours`x'
	quietly gen hour_sun_TFIT`x' = PV_productionkWh * (FIT_t=="T")* hours`x'
	quietly gen hour_sun_SFIT`x' = PV_productionkWh * (FIT_t=="S") * hours`x'
	quietly gen hour_sun_FFIT`x' = PV_productionkWh * (FIT_t=="F")* hours`x'			
	
	quietly	gen hour_virtual`x' = hours`x' * virtualincome	
	
	
	
	
	quietly gen hour_lagsun_PFIT`x' = lagged_2daysolar  * (FIT_t=="P")* hours`x'
	quietly gen hour_lagsun_TFIT`x' = lagged_2daysolar * (FIT_t=="T")* hours`x'
	quietly gen hour_lagsun_SFIT`x' = lagged_2daysolar * (FIT_t=="S") * hours`x'
	quietly gen hour_lagsun_FFIT`x' = lagged_2daysolar * (FIT_t=="F")* hours`x'	
	
			}

	quietly gen producing = (PV_production >0) & (PV_prod <.) & export ==0

	quietly gen producing_early =  (early_adopter==1)*  producing	

	quietly gen PV_early = PV_production * early_adopter

	
	quietly gen inc_weekend = weekend * virtualincome
	quietly gen sun_weekend = PV_production * weekend
 
 
 
	replace id = 1000*id
	gen household_hour = string(id) + string(hour)
	destring household_hour, replace
				


	
	** Regressions
	
	* Results table 2: 
	
set more off
quietly tsls consumption hour_hdd* hour_cdd* cross* (hour_price*  virtualincome  =  hour_sun_PFIT*  PV_production  hour_base*), fe(household_hour) cluster(id) demean
eststo est1
estimates save Results/Andrea/Baselineprice_`b', replace

quietly tsls consumption hour_hdd* hour_cdd* cross*  (hour_price*  virtualincome  =   hour_meanirrad_PV*  hour_base*), demean fe(household_hour) cluster(id)
eststo model
estimates save Results/Andrea/Price_PVirrad_`b', replace

quietly tsls consumption hour_hdd* hour_cdd* cross*  (hour_price*  virtualincome  =   hour_angle_PV*  hour_base*), demean fe(household_hour) cluster(id)
eststo model
estimates save Results/Andrea/Price_angle_`b', replace

	* Results table 3: 

quietly tsls consumption hour_hdd* hour_cdd*   PV_early  cross*  (virtualincome hour_price* = hour_lagsun_S*  hour_lagsun_T* hour_irrad* lagged_2daysolar hour_base* ),  cluster(id) demean fe(household_hour)
eststo last
estimates save Results/Andrea/early_adopters_`b', replace

quietly tsls consumption hour_hdd* hour_cdd*  cross* producing_early  (hour_price*  virtualincome  =   hour_lagsun_S*  hour_lagsun_T* hour_irrad* lagged_2daysolar hour_base*), cluster(id) demean fe(household_hour)
eststo prod
estimates save Results/Andrea/Producing_`b' , replace

quietly tsls consumption hour_hdd* hour_cdd* cross* (import_early  hour_price*  virtualincome  = hour_lagsun_S*  hour_lagsun_T* hour_irrad* lagged_2daysolar hour_base*), cluster(id) demean fe(household_hour)
eststo importing
estimates save Results/Andrea/Importing_`b' , replace

	* Appendix A table 4 (net import and lagged IV)
	
quietly tsls net_import hour_hdd* hour_cdd* PV_production cross*  (virtualincome hour_price* = hour_ghi_PFIT* hour_lagghi_PFIT* lagged_2dayghi ghi hour_base* ),  cluster(id) fe(household_hour) demean
eststo net
estimates save Results/Andrea/Netimport_`b'  , replace
		
quietly tsls consumption hour_hdd* hour_cdd* cross*  (virtualincome hour_price* =  hour_ghi_PFIT* hour_lagghi_PFIT* lagged_2dayghi ghi hour_base* ),  cluster(id) fe(household_hour) demean 
eststo lagged
estimates save Results/Andrea/LaggedIV_`b'  , replace
 	
	* Appendix A table 5 col 1 (non-solar income,  note appendix table 5 used to be bootstrap results)
	
quietly tsls consumption hour_hdd* hour_cdd*  median_HHincome cross* zones* hourfeed*  (hour_price*  virtualincome  =  hour_sun_PFIT*  PV_production hour_base* ), cluster(id)
eststo nsincome
estimates save Results/Andrea/NSincome_`b' , replace


	* Appendix A table 6 (temperature specifications)
quietly tsls consumption hddhours* cddhours*  cross* (hour_price* virtualincome = hour_sun_PFIT*  PV_production hour_base*), cluster(account_number) fe(household_hour) demean 
eststo fittemp
estimates save Results/Andrea/Heterotemp_`b', replace

quietly tsls consumption hour_hdd* hour_sqhdd* hour_cdd* hour_sqcdd* (hour_price*  virtualincome =  hour_sun_PFIT*  PV_production  hour_base* ), fe(household_hour) cluster(id) demean
eststo temp
estimates save Results/Andrea/Temp_ddsq_`b' , replace

quietly tsls consumption hour_hdd* hour_cdd* hour_temp_ghi* (hour_price*  virtualincome =  hour_sun_PFIT*  PV_production  hour_base* ), fe(household_hour) cluster(id) demean
eststo model2
estimates save Results/Andrea/Tempghi_`b' , replace
	
	* Appendix A table 7 (hourly income effects)
	
quietly tsls consumption hour_hdd* hour_cdd*  cross* ( hour_price* hour_virtual*  = hour_sun_PFIT* hour_sun_FFIT* PV_production hour_base* ),  cluster(id) demean fe(household_hour)
eststo hourincome
estimates save Results/Andrea/Hour_income_`b' , replace
	
	* Appendix A table 8 (opportunity cost)
	
quietly tsls consumption hour_hdd* hour_cdd* cross* (hour_peakoffpeak* virtualincome hour_ocp* =  hour_sun_PFIT*  PV_production  hour_base*), fe(household_hour) cluster(id) demean
eststo model 
estimate save Results/Andrea/OC_`b', replace

	* Appendix A Table 5 col 2. (weekend)

	drop if holiday==1

quietly tsls consumption hour_hdd* hour_cdd* cross* (hour_price*  virtualincome inc_weekend  =  hour_sun_PFIT*  PV_production  hour_base* sun_weekend) , fe(household_hour) cluster(id) demean
eststo model
estimates save Results/Andrea/Weekendinc_`b' , replace
	
	*/

	
	}
